#!/usr/bin/env python
#coding:utf-8
import sys
import os

def get_FileSize(file_path):
	file_path = unicode(file_path, 'utf8')
	if False == os.path.isfile(file_path):
		return 0
	else:
		return os.path.getsize(file_path)
#	fsize = fsize/float(1024*1024)
#	return round(fsize,2)

def get_local_file_size(path_root):
	files_dict = {}
	for root, dirs, files in os.walk(path_root):
		for file in files:
			fpath = root + '/' + file
			fsize = get_FileSize(fpath)
			files_dict[file] =[fsize, fpath]
	return files_dict


def get_bos_file_size(file_path):
	files_dict = {}
	with open(file_path, 'r') as f:
		# 文件格式如下
		#   2019-01-05 16:22:15       321154421     STANDARD  20161123_all about that bass_杨帆+阎妍+韩旭.mp4
		lines = f.readlines()
		for line in lines:
			line = line.rstrip('\n')    # 去掉结尾的回车符
			fsize = line[27:38]
			fname = line[52:]
			files_dict[fname] = int(fsize)

	return  files_dict


def main(bos_path, local_paths):
	local_files = {}
	for i in local_paths:
		#local_files = get_local_file_size(local_path)
		local_files.update(get_local_file_size(i))
	bos_files = get_bos_file_size(bos_path)   #'/home/work/liangquan/a')

	stats = [0, 0, 0, 0]    # total, ok, fail, not find

	print '\n\n查找BOS上的文件和本地文件是否一致：'
	# 如果要按照文件名排序输出，则用如下部分
	bos_sort = [ [k, bos_files[k] ] for k in sorted(bos_files.keys())]
	for item in bos_sort:
		k = item[0]
		v = item[1]
#	for k, v in bos_files.items():

		stats[0] += 1
		if local_files.has_key(k) :
			if v == local_files[k][0]:
#				print 'OK,       ' , k
				stats[1] += 1
			else:
				print v, local_files[k][0]
				print 'Failed,   ' , k
				stats[2] += 1
		else:
			print 'Not find, ' , k
			stats[3] += 1

	print 'BOS文件中，共 %d 个, 正确传输 %d 个, 异常 %d 个, 非本地文件 %d 个.' % (stats[0], stats[1], stats[2], stats[3])


	f = open('upload.sh', 'w')  # 若是'wb'就表示写二进制文件
	f.write('#!/bin/bash\n')
	f.write('set -x\n\n')
	print '\n\n查找本地目录中有哪些文件没有在BOS上：'
	stats = [0, 0, 0, 0]    # total, ok, fail, not find
	local_sort = [ k for k in sorted(local_files.keys())]
	for k in local_sort:
		stats[0] += 1
		if bos_files.has_key(k):
			stats[1] += 1
#			print '已上传!        ', k
		else:
			str = '~/bce-cli-0.10.8-centos6.5/bce bos cp \"' + local_files[k][1] + '\" bos:/musicwuliao/mv/buyaoyinyue/\n'
			f.write(str)
			print '未上传或上传失败!', k
			stats[2] += 1
	f.write('\n\n')
	f.close()
	print '本地文件中，共 %d 个, 其中已上传 %d 个, 未上传 %d 个.' % (stats[0], stats[1], stats[2])


if __name__ == "__main__":
	argc = len(sys.argv)
	if 2 == argc and ('-v' == sys.argv[1] or '-h' == sys.argv[1]):
		print '\n\t command line is like \'python %s [bos_path] [local_path...]\'\n' % __file__
		print '\t\t bos_path : bos path, like \'bos:/musicwuliao/mv/buyaoyinyue\', default a'
		print '\t\t local_path: default .\n\n'
	else:
		bos_path   = sys.argv[1] if argc >= 2 else 'a'
		#local_path = sys.argv[2] if argc >= 3 else os.curdir
		local_paths = []
		if argc < 3 :
			local_paths.append(os.curdir)
		else:
			for i in range(2, argc):
				local_paths.append(sys.argv[i])#unicode(sys.argv[i], 'utf-8') )
		for i in local_paths: print i
		main(bos_path, local_paths)


# 检查BOS上文件和本地文件大小是否存在差异
#   先手动用命令将bos上的文件存入本地文件中，在进行比较
# BOS上文件可以用这个命令导出
#   ~/bce-cli-0.10.8-centos6.5/bce bos ls bos:/musicwuliao/mv/buyaoyinyue/ > a; sort a > b; cat b;
# 上传BOS文件的命令
#   nohup ~/bce-cli-0.10.8-centos6.5/bce bos cp -r 2016/ bos:/musicwuliao/mv/buyaoyinyue/ > nohup.log 2>&1 &
# 脚本执行命令
#   python checkfile.py b buyaoyinyue/2016 python checkfile.py b buyaoyinyue/2017 python checkfile.py buyaoyinyue/兰州花絮
#